﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class login : System.Web.UI.Page
{
    private EncryptMD5 encryptMD5 = null;
    private Connection myConnection = null;
    private SQLQuery sqlQuery = null;
    private string TABLE_CANDIDATE = "Candidate";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["candidate"] != null)
        {
            Response.Redirect("index.aspx");
        }
        myConnection = new Connection();
        encryptMD5 = new EncryptMD5();
        sqlQuery = new SQLQuery();
    }

    protected void btn_login_Click(object sender, EventArgs e)
    {
        string email = txt_email.Text.Trim();
        string pass = encryptMD5.encrypt(txt_password.Text.Trim());
        if (myConnection.loginCandidate(email, pass))
        {
            LocalVariables.HIDDEN_LOGO = false;
            Session.Timeout = 60000;
            Session["candidate"] = email;
            Response.Redirect("profiles.aspx");
        }
        else
        {
            lbl_msg.Text = LocalVariables.CONNECT_FAILURE;
        }
    }

    protected void btn_send_Click(object sender, EventArgs e)
    {
        string email = txt_email_fpass.Text.Trim();
        if (sqlQuery.selectEmailIsExits(email, TABLE_CANDIDATE))
        {
            string newpass = LocalVariables.randomPassword();
            if (sqlQuery.updateChangePassword(email.Trim(), encryptMD5.encrypt(newpass.Trim())))
            {
                string title = "Forget password - Online Aptitude Test";
                string content = "New password : " + newpass.Trim();
                if (MailMessages.sendMail(email, title, content))
                {
                    lbl_msg.Text = LocalVariables.NEW_PASSWORD_SEND_MAIL;
                }
            }
        }
        else
        {
            lbl_msg.Text = LocalVariables.EMAIL_DOES_NOT_EXITST;
        }
    }
}